package cn.cms.controller;

import cn.cms.webcommon.ResBo;
import cn.cms.model.SysMenu;
import cn.cms.service.SysMenuService;
import cn.cms.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/sysmenu")
public class SysMenuController extends AbstractController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    SysMenuService sysMenuService;

    @RequestMapping("/page")
    public String page(HttpServletResponse response, Model model, String keyWord) {
        model.addAttribute("keyWord", keyWord);
        return "/sysmenu/page";
    }

    @RequestMapping("/list")
    @ResponseBody
    public void list(HttpServletResponse response) {
        ResBo resBo = new ResBo();
        List<SysMenu> sysMenus = sysMenuService.getMenus();
        resBo.addResult("data", sysMenus);
        ResponseUtil.printAjaxPack(response, resBo);
    }

    @RequestMapping("/edit")
    public String edit(HttpServletResponse response, Model model, Integer id) {
        if (id != null) {
            SysMenu sysMenu = sysMenuService.getMenuById(id);
            model.addAttribute("sysMenu", sysMenu);
        }
        return "sysmenu/form";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public void delete(HttpServletResponse response, SysMenu sysMenu) {
        ResBo resBo = new ResBo();
        if (sysMenu.getId() != null) {
            sysMenuService.delete(sysMenu);
        } else {
            resBo.setSuccess(false);
            resBo.setErrMsg("该项不存在或已经删除！");
        }
        ResponseUtil.printAjaxPack(response, resBo);
    }

    @RequestMapping(value = "/save")
    @ResponseBody
    public void save(HttpServletResponse response, SysMenu sysMenu) {
        ResBo resBo = new ResBo();
        Date date = new Date();
        SysMenu sysMenuSave;
        if (sysMenu.getId() == null) {// 增加
            sysMenuSave = sysMenu;
            sysMenuSave.setStatus(SysMenu.STATUS_ENABLE);
            sysMenuSave.setCreateTime(date);
            sysMenuSave.setUpdateTime(date);

            int maxSort = sysMenuService.getMaxSort(sysMenu.getPid());
            sysMenuSave.setSort(maxSort + 1);
            sysMenuService.save(sysMenuSave);
        } else {// 修改
            sysMenuSave = sysMenuService.getMenuById(sysMenu.getId());
            sysMenuSave.setUpdateTime(date);
            sysMenuSave.setMenuName(sysMenu.getMenuName());
            sysMenuSave.setPath(sysMenu.getPath());
            sysMenuSave.setDomain(sysMenu.getDomain());
            sysMenuSave.setIcon(sysMenu.getIcon());
            sysMenuSave.setSort(sysMenu.getSort());
            sysMenuSave.setNote(sysMenu.getNote());
            sysMenuService.update(sysMenuSave);
        }
        ResponseUtil.printAjaxPack(response, resBo);
    }

}